import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '../views/layout.vue'
// import detail from '../views/detail.vue'
// import login from '../views/login.vue'
// import register from '../views/register.vue'
// import article from '../views/article.vue'
// import collect from '../views/collect.vue'
// import like from '../views/like.vue'
// import user from '../views/user.vue'
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/detail/:id',
    component: () => import('../views/detail.vue')
  },
  {
    path: '/login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/register',
    component: () => import('../views/register.vue')
  },
  {
    path: '/',
    component: layout,
    redirect: '/article',
    children: [
      {
        path: 'article',
        component: () => import('../views/article.vue')
      },
      {
        path: 'collect',
        component: () => import('../views/collect.vue')
      },
      {
        path: 'like',
        component: () => import('../views/like.vue')
      },
      {
        path: 'user',
        component: () => import('../views/user.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    const whiteList = ['/register', '/login']
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      Toast.fail('你还没登陆呢')
    }
  }
})

export default router
